// 这里是写vuex的action
import axios from '@/api/request'
export default {
    /**
     * 选中单个商品
     * @param {*} param0
     * @param {*} option
     */
    enSlectShop({
        commit
    }, option) {
        commit('updateCartShopSelect', {
            ...option,
            playload: true
        })
        return false
    },
    /**
     * 取消选中单个商品
     * @param {*} param0
     * @param {*} option
     */
    deSlectShop({
        commit
    }, option) {
        commit('updateCartShopSelect', {
            ...option,
            playload: false
        })
        
        return false
    },
     /**
     * 一个商品的删除
     *  @param {any} {commit, state}
     * @param {*} param1
     */
    onButtonClick({
        commit
    },{shopCarId,shopIndex,dianId}){
        var delId=new Array(shopCarId)
        axios('device/shopCar/delete', 'post',{'ids':delId}).then(res => {   //数据删除
            console.log(res)
            if (res.data.code == 1000) {
                console.log(res)
            }
        })
        commit('delShopCart', { 
            dianId,
           shopIndex
         })
    },

    
    /**
     * 一个商品的数量自增
     * @param {*} param0
     * @param {*} param1
     */
    incrementShopCount({
        commit
    }, { dianId, shopIndex, count ,shopCarId}) {
        commit('updateCartShopCount', {
            dianId,
            shopIndex,
            playload: ++count
        })
        axios('device/shopCar/updateCount', 'post',{'id':shopCarId,count:count}).then(res => {   //数据删除
            console.log(res)
            if (res.data.code == 1000) {
                console.log(res)
            }
        })
    },
    /**
     * 一个商品的数量自减,数据更新
     * @param {*} param0
     * @param {*} param1
     */
    decrementShopCount({
        commit
    }, { dianId, shopIndex, count ,shopCarId}) {
          
        commit('updateCartShopCount', {
            dianId,
            shopIndex,
            playload: --count
        })
        axios('device/shopCar/updateCount', 'post',{'id':shopCarId,count:count}).then(res => {   //数据删除
            console.log(res)
            if (res.data.code == 1000) {
                console.log(res)
            }
        })
    },
    /**
     * 切换单个商品选中状态
     * @param {*} param0
     * @param {*} param1
     */
    toggleSelect({
        dispatch
    }, { select, dianId, shopIndex }) {
        // let cur = state.catrs[option.dianId].shops[option.shopIndex].select;
        if (select) {
            dispatch('deSlectShop', { dianId, shopIndex })
        } else {
            dispatch('enSlectShop', { dianId, shopIndex })
        }
        return false
    },
    /**
     * 选中单个店铺的所有商品
     *
     * @param {any} {dispatch, state}
     * @param {any}dianId
     */
    enAllSelectForDian({
        dispatch,
        state
    }, dianId) {
        state
            .carts[dianId]
            .shops
            .forEach((shop, shopIndex) => {
                dispatch('enSlectShop', { dianId, shopIndex })
            })
    },
    /**
     * 取消选中单个店铺的所有商品
     *
     * @param {any} {dispatch, state}
     * @param {any}dianId
     */
    deAllSelectForDian({
        dispatch,
        state
    }, dianId) {
        state
            .carts[dianId]
            .shops
            .forEach((shop, shopIndex) => {
                dispatch('deSlectShop', { dianId, shopIndex })
            })
    },
    /**
     * 切换选中单个店铺的所有商品
     *
     * @param {any} {dispatch, state}
     * @param {any}dianId
     */
    toggleSeletForDian({
        getters,
        dispatch
    }, dianId) {
        if (getters.isAllSelectByDian(dianId)) {
            dispatch('deAllSelectForDian', dianId)
        } else {
            dispatch('enAllSelectForDian', dianId)
        }
    },
    /**
     * 切换选中单个店铺的所有商品
     *
     * @param {any} {dispatch, state}
     * @param {any}dianId
     */
    toggleAllSelect({ dispatch, state, getters }) {
        let isAllSelect = state.carts.every(dian => dian.shops.every(shop => shop.select))
        state
            .carts
            .forEach((dian, dianId) => {
                if (isAllSelect) {
                    dispatch('deAllSelectForDian', dianId)
                } else {
                    dispatch('enAllSelectForDian', dianId)
                }
            })
    },
     /**
     * 获取购物车
     * @param {any} {commit, state}
     * @param {*} param1
     */
    GetCars({ commit }, option) {
        axios('/device/shopCar/getShopCar/' + option, 'post', {}).then(res => {
            //  alert('进入action');
            if (res.data.code == 1000) {
                console.log(res)
                    // alert('action中调用封装后的axios成功');
                commit('UpCarts', res.data.data)
            }
        })
    },
}